/**
 * Created by Administrator on 2018/1/15.
 */
define(function(require, exports, module) {
    var $=require('zepto');
    cn=require('conment');
    api=require('api');
    copy=require('copy');
    qrcode=require('qrcode');
    module.exports = {
        int:{
            index:0,
            realityName:sessionStorage.getItem('realityName',true),
        },
        isLoding:true,

        msgActive:function (val) {
            switch (val){
                case 0:
                    return '连连支付'
                    break;
                case 1:
                    return '支付宝转账'
                    break;
                case 2:
                    return '支付宝快捷支付'
                    break;

            }
        },
        activePlay:function () {
            const  _this=this;
            $('.recharge-active').on('click',function () {
                $(this).addClass('is-active').siblings('div.recharge-active').removeClass('is-active');
                _this.int.index= $('.is-active').data('active');
            });
            $('#moneys').on('input propertychange',function () {
                var v=$(this).val()
                _this.onlyNumber(v,'#moneys')
            });
            $('#play-next').on('click',function () {
                var money=$('#moneys').val()
                _this.int.index= $('.is-active').data('active');
                if( _this.int.index==0){
                    if(_this.int.realityName=='false'){
                        cn.modelDialog({
                            msg:'请先实名认证',
                            btnValue:'取消',
                            btn:[{
                                txt:'去认证',
                                callBack:function(){
                                    window.location.href='/view/real_name.html'
                                }
                            }]
                        });
                    }else{
                        ///_this.lianlianPay(money)
                        window.location.href='/view/lianlian.html'
                    }
                }else if(_this.int.index==1){
                    window.location.href='/view/zhifubao.html'
                }else if(_this.int.index==2){
                    window.location.href='/view/recharge_o2o.html'
                }else if(_this.int.index==3){
                    window.location.href='/view/charge_page.html?type='+_this.int.index
                }else if(_this.int.index==4){
                    window.location.href='/view/transfer.html?type='+_this.int.index
                }else if(_this.int.index==5){
                    window.location.href='/view/yilian.html?type='+_this.int.index
                }

            })
        },
        copyBank:function () {
            var clipboard = new Clipboard('#copy_btn1');
            $('#target4').text(cn.getQueryString('money'))
            clipboard.on('success', function(e) {
                e.clearSelection();
                cn.alert('收款户名已复制到剪贴板')
            });
            var clipboard = new Clipboard('#copy_btn2');
            clipboard.on('success', function(e) {
                e.clearSelection();
                cn.alert('银行卡号已复制到剪贴板')
            });
            var clipboard = new Clipboard('#copy_btn3');
            clipboard.on('success', function(e) {
                e.clearSelection();
                cn.alert('开户行已复制到剪贴板')
            });
            var clipboard = new Clipboard('#copy_btn4');
            clipboard.on('success', function(e) {
                e.clearSelection();
                cn.alert('充值金额已复制到剪贴板')
            });
            $('#informBtn').on('click',function () {
                cn.Ajax(api.alipayNotice,{
                    account:'yhzz',
                },function(d){
                    if(d.flag==1){
                        cn.alert(d.message)
                        // cn.alert(d.message)
                    }else {
                        cn.alert(d.message)
                    }
                },'post')
            })
        },
        lianlianPay:function (m) {
            const _this=this;
            var $loading = $('<div class="loading-read"><img src="../images/home/loading.gif"/></div>')
            $("body").append($loading);
            cn.Ajax(api.lianlianPay,{
                amount:m,
                bci:'1',
            },function(d){
                $(".loading-read").remove();
                if(d.flag==1){
                    $('#lianlianDataUrl').attr('action',d.req_url);
                    $('#lianlianDateVal').val(d.req_data)
                    $('#lianlianDataUrl').submit()
                    // window.location.href=d.req_url
                }else{
                    cn.alert(d.message)
                }
            },'post')
        },
        ipsPay:function (m) {
            if(window.localStorage.getItem('iOSsessionid')){
                $.ajax({
                    url:api.user.ipsPay,
                    type:'post',
                    data:{
                        money:m
                    },
                    beforeSend:function(xhr){
                        xhr.setRequestHeader('FC-X-SID',window.localStorage.getItem('iOSsessionid'));
                    },
                    success:function(d){
                        d = JSON.parse(d)
                        if(d.code=1){
                            $('#parse').val(d.data.pGateWayReq)
                            $('#url').attr("action", d.data.action).submit()
                        }
                    },
                    error:function(){
                        alert('请求失败');
                    },
                    complete:function(){
                        window.localStorage.removeItem('iOSsessionid')
                    }

                })
            }else {
                cn.Ajax(api.user.ipsPay,{
                    money : m,
                },function(d){
                    if(d.code=1){
                        $('#parse').val(d.data.pGateWayReq)
                        $('#url').attr("action", d.data.action).submit()
                    }
                },'post')
            }
        },


        zhifubao_pay:function () {
          const _this=this;
          $('#informBtn').on('click',function () {
            const accounts=$('#account').val();
            if($.trim(accounts)==''||$.trim(accounts)==null){
                cn.alert('请输入您的账户');
                return false
            }
            cn.Ajax(api.alipayNotice,{
                account:accounts,
            },function(d){
                if(d.flag==1){
                    $('.tagMode').show();
                    // cn.alert(d.message)
                }else {
                    cn.alert(d.message)
                }
            },'post')

          })
            var targetText=$("#target").text();
            var clipboard = new Clipboard('#copy_btn');

            clipboard.on('success', function(e) {
                e.clearSelection();
                $('.tagMode').hide();
                cn.alert('复制成功')
            });
        },
        onlyNumber:function (obj,id){
            var t = obj.charAt(0);
            obj= obj.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
            obj = obj.replace(/^\./g,""); //验证第一个字符是数字
            obj = obj.replace(/\.{2,}/g,"."); //只保留第一个, 清除多余的
            obj = obj.replace('.','$#$').replace(/\./g,'').replace('$#$','.'); //// //保证.只出现一次，而不能出现两次以上
            $(id).val(obj);
        },
        init:function () {
            const _this=this;

            var targetText=$("#target").text();
            var clipboard = new Clipboard('#copy_btn');

            clipboard.on('success', function(e) {
                e.clearSelection();
                $('.tagMode').hide();
                cn.alert('复制成功')
            });
        },
        finance:function () {
            const _this=this;
            cn.Ajax(api.finance,{
            },function(d){
                if(d.flag==1){
                    $('#bindTel').text(d.username);
                }
            },'post')
        },
        rechargeQrCode_list:function () {
            const _this=this;
            cn.Ajax(api.rechargeQrCode_list,{
            },function(d){
                if(d.flag){
                    var list=d.list;
                    var html='';
                    $.each(list,function(i,l,array){
                        html+=' <div data-money="'+l.money+'" data-id="'+l.id+'" data-url="'+l.url+'" class="'+(i==0?'active':'')+'">100</div>'
                    });
                    $('#active-moneys').html(html);
                    $('#amount').text($('.money-s .active').text()+'元')
                    $('.money-s div').on('click',function () {
                        $(this).addClass('active').siblings('div').removeClass('active');
                        $('#amount').text($('.money-s .active').text()+'元')
                    });
                    $('#select-fangs>div').on('click',function () {
                        $(this).addClass('is-active').siblings('div').removeClass('is-active');
                    });
                }

            },'post')
        },
        o2oInit:function () {
            const _this = this;
            _this.finance();
            _this.rechargeQrCode_list()

            $('#chargeBtn_o2o').on('click',function () {
                _this.o2o()
            })
            $("#backType").click(function(){
                if($(".endAmountDiv").is(':visible')){
                    $(".step2").removeClass('active')
                    $(".editAmount").show().siblings().hide();
                    $('#qrcode').html('')
                }else{
                    window.location.href = './deposit_pay.html';
                }
            });
        },
        o2o:function () {
            const _this = this;
            var m=parseFloat($('.money-s .active').data('money'));
            var id=parseFloat($('.money-s .active').data('id'));
            var url=parseFloat($('.money-s .active').data('url'));

            var t=parseFloat($('#select-fangs .is-active').data('active'));
            $('#chargeBtn_o2o').removeClass('chargeBtn').attr('disabled',true)
            cn.Ajax(api.alipayRechrage,{
                qrId:id
            },function(d){
                $('#chargeBtn_o2o').addClass('chargeBtn').attr('disabled',false)
                if(d.flag==1){
                    $('#amountEnd').text(m.toFixed(2));
                    $(".step2").addClass('active');
                    $('#outer').text(m)
                    $(".editAmount").hide().siblings().show();
                    var qrcode = new QRCode("qrcode", {
                        text:d.rdUrl,
                        width: 150,
                        height: 150,
                        colorDark : "#000000",
                        colorLight : "#ffffff",
                        correctLevel : QRCode.CorrectLevel.H
                    });
                    $('#startAliPay').on('click',function () {
                         window.location.href =d.rdUrl;
                    })
                }else{
                    cn.alert(d.message)
                }
            },'post')
        }
    }

});